var page_num=1;
var competition_province;
var competition_city;
var competition_count;
var result;
var old_value="";//select2的选择
var competitionResult; //详细页面的
var gymnasiumResult;//详细页面的
$(function(){
	$.post('__URL__/listCompetitionByTime',{'page_num':page_num,'competition_province':'广州省','competition_city':'广州市'},function(resultList){
		page_num=resultList.page_num;
		competition_count=resultList.competition_count;
		result=resultList.result;
	    },'json');
	$('#contest').css({display:"block"});
	competition_main();
});
function competition_main(){
				$.each(result,function(i,item_X){
					// console.log(item_X);
					com_model_item=item_X["com_model_item"];
					console.log(com_model_item);
					var contest_type="单项赛";
					var contests=new Array();
					switch(com_model_item){
						case "1":
							contest_type="单项赛";
							contests.push("男单");
							break;
						case "2":
							contest_type="单项赛";
							contests.push("女单");
							break;
						case "3":
							contest_type="单项赛";
							contests.push("男双");
							break;
						case "4":
							contest_type="单项赛";
							contests.push("男单");
							break;
						case "5":
							contest_type="单项赛";
							contests.push("女双");
							break;
						case "6":
							contest_type="三体团体赛";
							contests.push("男双");
							contests.push("女双");
							contests.push("混双");
							break;
						case "7":
							contest_type="三体团体赛";
							contests.push("男双");
							contests.push("混双");
							contests.push("老年组");							
							break;
						case "8":
							contest_type="三体团体赛";
							contests.push("男双");
							contests.push("混双");
							contests.push("男单");							
							break;
						default:
							break;

					}
					competition_begin_time=item_X["competition_begin_time"];
					competition_contain_number=item_X["competition_contain_number"];
					competition_id=item_X["competition_id"];
					competition_name=item_X["competition_name"];
					competition_picture=item_X["competition_picture"];
					competition_publish_time=item_X["competition_publish_time"];
					competition_regist_unit=item_X["competition_regist_unit"];
					competition_status=item_X["competition_status"];
					// 将获取的数据显示在页面---------
					
					var contest_list="<ul><li><span>主办方：</span>某某球会</li>";
					contest_list +="<li><span>报名时间：</span>";
					contest_list +=competition_publish_time;
					contest_list +="<li><span>比赛时间：</span>";
					contest_list +=competition_begin_time;
					contest_list +="<li><span>竞赛类型：</span>";
					contest_list +=contest_type;
					contest_list +="<li><span>报名项目：</span><select class=\"form-control\">";
					$.each(contests,function(j,item){
						contest_list +="<option>"+item+"</option>";
					});
					contest_list +="</select></li>";
					contest_list +="<li><button class=\"atttend_match\">我要报名</button><input type=\"button\" class=\"know_more\" name=\"";
					contest_list += competition_id+"\" value=\"点击了解详情\"\/></li></ul>";
					console.log(contests);
					console.log(contest_list);
					console.log(contest_div);
					var contest_list_class=".contest_list"+i;
					var contest_div=$(contest_list_class);
					contest_div.html(contest_list);
				});
			}
			function competition_detail(){
				var competition_name;
				var com_model_item;
				var competition_begin_time;
				var competition_publish_time;
				var competition_picture;
				var competition_content;
				console.log(competitionResult);
				com_model_item=competitionResult["com_model_item"];
				var contest_type="单项赛";
				var contests=new Array();
				switch(com_model_item){
					case "1":
						contest_type="单项赛";
						contests.push("男单");
						break;
					case "2":
						contest_type="单项赛";
						contests.push("女单");
						break;
					case "3":
						contest_type="单项赛";
						contests.push("男双");
						break;
					case "4":
						contest_type="单项赛";
						contests.push("男单");
						break;
					case "5":
						contest_type="单项赛";
						contests.push("女双");
						break;
					case "6":
						contest_type="三体团体赛";
						contests.push("男双");
						contests.push("女双");
						contests.push("混双");
						break;
					case "7":
						contest_type="三体团体赛";
						contests.push("男双");
						contests.push("混双");
						contests.push("老年组");							
						break;
					case "8":
						contest_type="三体团体赛";
						contests.push("男双");
						contests.push("混双");
						contests.push("男单");							
						break;
					default:
						break;

				}
				competition_begin_time=competitionResult["competition_begin_time"];
				competition_publish_time=competitionResult["competition_publish_time"];
				competition_name=competitionResult["competition_name"];
				competition_content=competitionResult["competition_content"];
				competition_picture=competitionResult["competition_picture"];
				$('.introduce_contest img').attr('src',competition_picture);
				var introduce_body="<span>球赛介绍：</span><br/><div><p>";
				introduce_body+=competition_content;
				introduce_body+="</p></div><span>主办方介绍</span><br/><div><p>羽乐是一款便于羽毛球兴趣协会（球会）组织工作的工具软件为切入，聚合球会会员社交，球场O2O等多项功能的app软件。提供会员创建球会，组织活动，活动报名，活动积分，资料共享，会员聊天，球场订场，教练培训等多项功能，是羽毛球爱好者、球场经营者，羽球教练深度使用的应用软件</p></div>";
				$('.introduce_contest .content').html(introduce_body);
				var contest_detail="<ul><li><span>竞赛名称：</span>"+competition_name+"</li><li><span>报名时间：</span>";
				contest_detail+=competition_publish_time;
				contest_detail+="</li><li><span>比赛时间：</span>";
				contest_detail+=competition_begin_time;
				contest_detail+="</li><li><span>竞赛类型:</span>";
				contest_detail+=contest_type;
				contest_detail+="</li><li><span>报名项目:</span><select> class=\"form-control\">";
				$.each(contests,function(i,item){
					contest_detail +="<option>"+item+"</option>";
				});
				contest_detail+="</select></li><li><button class=\"atttend_match\">我要报名</button></li></ul>";
				var contest_div=$('.contest_detail');
				contest_div.html(contest_detail);
			}
			function atttend_match(competition_id){
				$.post('__URL__/competitionApply',{'competition_id':competition_id},function(result){
						switch(result){
							case 0:
								alert("已截至报名");
								break;
							case 1:
								alert("报名成功");
								break;
							case 2:
								alert("已经报过名了");
								break;
							default:
								break;
						}
	    		},'json');
			}
						$(".contest_messege").delegate(".know_more","click",function(){
				var competition_id=$(this).attr('name');
				$(".court_nav").css({display:"none"});
				$(".introduce_contest").css({display:"block"});
				$("#contest").css({display:"none"});
				$("#which_contest").css({display:"block"});
				$('.header_nav a').attr("href","<{:U('Competition/index')}>");
				$('.header_nav span').html('竞赛详情页面');
				$.post('__URL__/competitionInfo',{'competition_id':competition_id},function(resultList){
						competitionResult=resultList.competitionResult;
						gymnasiumResult=resultList.gymnasiumResult;
						result=resultList.userResult;
						// console.log(competitionResult);
						console.log(gymnasiumResult);
						console.log(result);
						competition_detail();
	    		},'json');
	    		
			});

			$('.select2').bind('click',function(event){
				var now_value=$('.select2 option:selected').text();
				var com_model_item;
				var competition_begin_time;
				var competition_contain_number;
				var competition_id;
				var competition_name;
				var competition_picture;
				var competition_publish_time;
				var competition_regist_unit;
				var competition_status;
				if(old_value!=now_value){
					old_value=now_value;
					competition_province=$('.select1 option:selected').text();
					competition_city=$('.select2 option:selected').text();
					$.post('__URL__/listCompetitionByTime',{'page_num':page_num,'competition_province':competition_province,'competition_city':competition_city},function(resultList){
						page_num=resultList.page_num;
						competition_count=resultList.competition_count;
						result=resultList.result;
	    			},'json');
					$('#contest').css({display:"block"});
					competition_main();
				}
				$('.atttend_match').bind('click',competition_id,function(){
					atttend_match(competition_id);
				});
			});
